package com.heartline.interceptor;

import com.blade.exception.NotFoundException;
import com.blade.ioc.annotation.Bean;
import com.blade.mvc.WebContext;
import com.blade.mvc.handler.DefaultExceptionHandler;
import com.heartline.exception.Err;
import com.heartline.exception.ServiceException;
import com.heartline.pojo.RestResponse;
import lombok.extern.slf4j.Slf4j;

/**
 * 全局异常处理
 * Created by zhy on 2018/6/7.
 */
@Slf4j
@Bean
public class GolbalExceptionHandler extends DefaultExceptionHandler {

    @Override
    public void handle(Exception e) {
        if (e instanceof ServiceException) {
            ServiceException serErr = (ServiceException) e;
            log.error("业务异常: {}", serErr.msg());
            WebContext.response().json(RestResponse.fail(serErr.code(), serErr.msg()));
        }else if (e instanceof NotFoundException){
        } else {
            log.error("服务器异常: {}", e.getMessage(), e);
            WebContext.response().json(RestResponse.fail(Err.SYS_ERROR.code(), Err.SYS_ERROR.msg()));
        }
    }

}